package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.Category;
import com.leyou.item.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;

/**
 * @package com.leyou.item.service
 * @description:
 * @author: 86157
 * @date 2019/6/23 19:34
 * @version: V1.0
 */
@Service
public class CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    public List<CategoryDTO> queryListByParent(Long pid) {
        // 查询条件，mapper会把对象中的非空属性作为查询条件
        Category t = new Category();
        t.setParentId(pid);
        List<Category> list = categoryMapper.select(t);
        // 判断结果
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        // 使用自定义工具类，把Category集合转为DTO的集合
        return BeanHelper.copyWithCollection(list, CategoryDTO.class);
    }


    /**
     * 根据brand的id查询category
     *
     * @param id
     * @return
     */
    public List<CategoryDTO> queryCategoryByBrand(Long id) {
        //根据brand的id查询category，自定义mapper查询
        List<Category> list = categoryMapper.queryCategoryByBrand(id);
        System.out.println("list = " + list);
        List<CategoryDTO> categoryDTOS = BeanHelper.copyWithCollection(list, CategoryDTO.class);
        System.out.println("categoryDTOS = " + categoryDTOS);
        return categoryDTOS;
    }

    /**
     * 根据分类id查询分类
     *
     * @return
     */
    public List<CategoryDTO> queryCategoryByCid(List<Long> ids) {
        List<Category> list = categoryMapper.selectByIdList(ids);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }

        return BeanHelper.copyWithCollection(list, CategoryDTO.class);

    }
}